﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Faker {
    public static class ArrayExtensions {
        
        public static T Random<T>(this T[] arr) {
            var r = new Random();
            return arr[r.Next(arr.Length)];
        }

        public static T[] Shuffle<T>(this T[] arr) {
            var random = new Random();
            var list = new List<KeyValuePair<int, T>>();
            
            // Add all items from array
            // Add new random int each time
            foreach (T i in arr) {
                list.Add(new KeyValuePair<int, T>(random.Next(), i));
            }
            
            // Sort the list by the random number
            var sorted = from item in list
                         orderby item.Key
                         select item;
            
            // Allocate new item array
            var result = new T[arr.Length];
            
            // Copy values to array
            int index = 0;
            foreach (KeyValuePair<int, T> pair in sorted) {
                result[index] = pair.Value;
                index++;
            }
            
            // Return copied array
            return result;
        }
    }
}
